package top.zywork.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.zywork.common.BeanUtils;
import top.zywork.common.DateParseUtils;
import top.zywork.common.PageQueryUtils;
import top.zywork.common.StringUtils;
import top.zywork.dos.DefaultDistributionConfig;
import top.zywork.dto.PagerDTO;
import top.zywork.enums.DatePatternEnum;
import top.zywork.enums.SysConfigEnum;
import top.zywork.query.DistributionUserQuery;
import top.zywork.security.SecurityUtils;
import top.zywork.service.DistributionService;
import top.zywork.service.SysConfigService;
import top.zywork.vo.DistributionUserVO;
import top.zywork.vo.PagerVO;
import top.zywork.vo.ResponseStatusVO;

/**
 * 分销数据查询控制器<br/>
 *
 * 创建于2018-12-28<br/>
 *
 * @author 王振宇
 * @version 1.0
 */
@RestController
@RequestMapping("/distribution")
@Slf4j
public class DistributionController extends BaseController {

    private DistributionService distributionService;

    private SysConfigService sysConfigService;

    /**
     * 查询顶级分销商
     *
     * @return
     */
    @PostMapping("admin/all-top")
    public ResponseStatusVO listAllTop(@RequestBody DistributionUserQuery distributionUserQuery) {
        PagerDTO pagerDTO = distributionService.listAllTop(distributionUserQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), DistributionUserVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 分页查询指定用户上面的几级经销商
     *
     * @param levels
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("user/above")
    public ResponseStatusVO listAboveUsers(Long[] levels, Integer pageNo, Integer pageSize) {
        return listAboveUsers(SecurityUtils.getJwtUser().getUserId(), levels, pageNo, pageSize);
    }

    /**
     * 分页查询指定用户上面的几级经销商
     *
     * @param userId
     * @param levels
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("admin/above")
    public ResponseStatusVO listAboveUsers(Long userId, Long[] levels, Integer pageNo, Integer pageSize) {
        DefaultDistributionConfig defaultDistributionConfig = sysConfigService.getByName(SysConfigEnum.DEFAULT_DISTRIBUTION_CONFIG.getValue(), DefaultDistributionConfig.class);
        PagerDTO pagerDTO = distributionService.listAboveUsers(userId, defaultDistributionConfig.getDistributionLevel(), levels, PageQueryUtils.getPageQuery(pageNo, pageSize));
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), DistributionUserVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 分页查询指定用户的直接上级经销商
     *
     * @param userId
     * @return
     */
    @PostMapping("admin/direct-above")
    public ResponseStatusVO listDirectAboveUsers(Long userId, Integer pageNo, Integer pageSize) {
        DefaultDistributionConfig defaultDistributionConfig = sysConfigService.getByName(SysConfigEnum.DEFAULT_DISTRIBUTION_CONFIG.getValue(), DefaultDistributionConfig.class);
        PagerDTO pagerDTO = distributionService.listDirectAboveUsers(userId, defaultDistributionConfig.getDistributionLevel(), (long) (defaultDistributionConfig.getDistributionLevel() - 1), PageQueryUtils.getPageQuery(pageNo, pageSize));
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), DistributionUserVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 分页查询指定用户的直接上级经销商
     *
     * @return
     */
    @PostMapping("user/direct-above")
    public ResponseStatusVO userListDirectAboveUsers(Integer pageNo, Integer pageSize) {
        return listDirectAboveUsers(SecurityUtils.getJwtUser().getUserId(), pageNo, pageSize);
    }

    /**
     * 分页查询指定用户下面的几级经销商
     *
     * @param levels
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("user/below")
    public ResponseStatusVO listBelowUsers(Long[] levels, Integer pageNo, Integer pageSize) {
        return listBelowUsers(SecurityUtils.getJwtUser().getUserId(), levels, pageNo, pageSize);
    }

    /**
     * 分页查询指定用户下面的几级经销商
     *
     * @param userId
     * @param levels
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("admin/below")
    public ResponseStatusVO listBelowUsers(Long userId, Long[] levels, Integer pageNo, Integer pageSize) {
        PagerDTO pagerDTO = distributionService.listBelowUsers(userId, levels, PageQueryUtils.getPageQuery(pageNo, pageSize));
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), DistributionUserVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 分页查询指定用户的直接下级经销商
     *
     * @param userId
     * @return
     */
    @PostMapping("admin/direct-below")
    public ResponseStatusVO listDirectBelowUsers(Long userId, Integer pageNo, Integer pageSize) {
        PagerDTO pagerDTO = distributionService.listDirectBelowUsers(userId, 2L, PageQueryUtils.getPageQuery(pageNo, pageSize));
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), DistributionUserVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 分页查询指定用户的直接下级经销商
     *
     * @return
     */
    @PostMapping("user/direct-below")
    public ResponseStatusVO userListDirectBelowUsers(Integer pageNo, Integer pageSize) {
        return listDirectBelowUsers(SecurityUtils.getJwtUser().getUserId(), pageNo, pageSize);
    }

    /**
     * 获取配置的分销等级
     * @return
     */
    @GetMapping("admin/distribution-level")
    public ResponseStatusVO getDistributionLevel() {
        DefaultDistributionConfig defaultDistributionConfig = sysConfigService.getByName(SysConfigEnum.DEFAULT_DISTRIBUTION_CONFIG.getValue(), DefaultDistributionConfig.class);
        return ResponseStatusVO.ok("查询成功", defaultDistributionConfig.getDistributionLevel());
    }

    /**
     * 查询指定用户下每个等级分别有多少分销商
     * @param levels
     * @return
     */
    @PostMapping("admin/below-users-per-level/{userId}")
    public ResponseStatusVO countBelowUsersPerLevel(@PathVariable("userId") Long userId, @RequestBody Long[] levels) {
        return ResponseStatusVO.ok("查询成功", distributionService.countBelowUsersPerLevel(userId, levels));
    }

    /**
     * 用户查询指定用户下每个等级分别有多少分销商
     * @param levels
     * @return
     */
    @PostMapping("user/below-users-per-level")
    public ResponseStatusVO userCountBelowUsersPerLevel(Long[] levels) {
        return countBelowUsersPerLevel(SecurityUtils.getJwtUser().getUserId(), levels);
    }

    /**
     * 获取指定用户编号下级最大层级数
     * @param userId
     * @return
     */
    @GetMapping("admin/max-level/{userId}")
    public ResponseStatusVO getMaxLevel(@PathVariable("userId") Long userId) {
        return ResponseStatusVO.ok("查询成功", distributionService.getMaxLevel(userId));
    }

    /**
     * 根据用户编号获取该用户所有下级总个数（团队计数）
     * @param userId
     * @return
     */
    @GetMapping("admin/count-all-below-users/{userId}")
    public ResponseStatusVO countAllBelowUsers(@PathVariable("userId") Long userId) {
        return ResponseStatusVO.ok("查询成功", distributionService.countAllBelowUsers(userId));
    }

    /**
     * 获取指定用户下的指定等级创造的商城订单总业绩
     * @param userId 用户编号
     * @param levels 分销等级，如"2,3,4"
     * @param beginTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @PostMapping("admin/count-mall-below-performance")
    public ResponseStatusVO countMallBelowPerformance(Long userId, String levels, String beginTime, String endTime) {
        return ResponseStatusVO.ok("指定下级的总业绩", distributionService.countMallBelowPerformance(userId, StringUtils.strToLongArray(levels, ","),
                org.apache.commons.lang3.StringUtils.isEmpty(beginTime) ? null : DateParseUtils.parseDate(beginTime, DatePatternEnum.DATETIME.getValue()),
                org.apache.commons.lang3.StringUtils.isEmpty(endTime) ? null : DateParseUtils.parseDate(endTime, DatePatternEnum.DATETIME.getValue())));
    }

    /**
     * 获取指定用户下的整个团队创造的商城订单总业绩
     * @param userId 用户编号
     * @param beginTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @PostMapping("admin/count-mall-all-below-performance")
    public ResponseStatusVO countMallAllBelowPerformance(Long userId, String beginTime, String endTime) {
        return ResponseStatusVO.ok("团队总业绩", distributionService.countMallAllBelowPerformance(userId,
                org.apache.commons.lang3.StringUtils.isEmpty(beginTime) ? null : DateParseUtils.parseDate(beginTime, DatePatternEnum.DATETIME.getValue()),
                org.apache.commons.lang3.StringUtils.isEmpty(endTime) ? null : DateParseUtils.parseDate(endTime, DatePatternEnum.DATETIME.getValue())));
    }

    @GetMapping("admin/to-top/{userId}")
    public ResponseStatusVO putToTop(@PathVariable("userId") Long userId) {
        return distributionService.putToTop(userId);
    }

    @GetMapping("admin/to-user/{userId}/{toUserId}")
    public ResponseStatusVO putToUser(@PathVariable("userId") Long userId, @PathVariable("toUserId") Long toUserId) {
        return distributionService.putToUser(userId, toUserId);
    }

    @Autowired
    public void setDistributionService(DistributionService distributionService) {
        this.distributionService = distributionService;
    }

    @Autowired
    public void setSysConfigService(SysConfigService sysConfigService) {
        this.sysConfigService = sysConfigService;
    }
}
